﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Notepad
{
    public partial class MainForm : Form
    {
        private string formTitle = "Notepad {0}";
        private bool hasChanges = false;
        private bool justOpened = false;
        private string currentFileName = "Untitled";

        public MainForm()
        {
            InitializeComponent();
            Text = formTitle.Replace("{0}", "Untitled");
            txtNotepad.TextChanged += new EventHandler(txtNotepad_TextChanged);
        }

        private void txtNotepad_TextChanged(object sender, EventArgs e)
        {
            if (justOpened)
            {
                justOpened = !justOpened;
            }
            else
            {
                if (!hasChanges)
                {
                    hasChanges = true;
                    Text = formTitle.Replace("{0}", "*" + currentFileName);
                }
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Text Files (*.txt)|*.txt";

            DialogResult result = ofd.ShowDialog();
            if (result == DialogResult.OK)
            {
                using (TextReader reader = new StreamReader(ofd.FileName))
                {
                    justOpened = !justOpened;
                    txtNotepad.Text = reader.ReadToEnd();
                    reader.Close();
                }
                hasChanges = false;
                currentFileName = ofd.FileName;
                Text = formTitle.Replace("{0}", currentFileName);
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.DefaultExt = ".txt";
            sfd.Filter = "Text Files (*.txt)|*.txt";

            DialogResult result = sfd.ShowDialog();
            if (result == DialogResult.OK)
            {
                using (TextWriter writer = new StreamWriter(sfd.FileName))
                {
                    writer.Write(txtNotepad.Text);
                    writer.Close();
                }
                hasChanges = false;
                currentFileName = sfd.FileName;
                Text = formTitle.Replace("{0}", currentFileName);
            }
        }

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.Font = txtNotepad.Font;
            DialogResult dg = fd.ShowDialog();
            if (dg == DialogResult.OK)
            {
                txtNotepad.Font = fd.Font;
            }
        }

        private void printToPrinterToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (hasChanges)
            {
                
            }
            Close();
        }
    }
}
